#!/bin/bash

#
# script to run Elda regression tests
#

function multi_diff() {
  if [[ "$1" == *json* ]] ; then
    java -classpath $(cat json-diff-classpath):/home/chris/SPOO/Temp/target/classes run.jd $1 $2
  else
    diff $1 $2
  fi
}

export _PORT=1829
export _JAR=no
export _REBUILD=no
export _CLEAN=no

for x in $* ; do
  export _$x
done

echo
echo ";;; Build current jar: " $_JAR
echo ";;; Elda port:         " $_PORT
echo ";;; Rebuild outputs:   " $_REBUILD
echo ";;; Clean first:       " $_CLEAN

mkdir -p this-elda
mkdir -p gold results differences meta_differences gold_differences

if [ ${_CLEAN} == "yes" ]; then
  rm -rf gold/* 
  rm -rf results/* 
  rm -rf differences/*
  rm -rf gold_differences/*
  rm -rf meta_differences/*
fi

rm -f servers.log

if [ $_JAR == "yes" ]; then
  echo "Building current (probably snapshot) jar"
  (cd ..; mvn clean package)
  mv ../elda-standalone/target/elda-standalone-*.jar this-elda
fi

if [ $_REBUILD == "yes" ]; then
  echo
  echo ";;; preparing golden results and differences ================================================"
  echo
#
  for elda in eldas/*.jar
  do
    for config_file in configs/*.ttl
    do
      echo 
      echo ";;; configuration" ${config_file} "-----------------------------------------------"
      echo
      export config=$(echo $config_file | sed -e 's:.*/::' -e 's/\.ttl$//')
      export e_name=$(echo $elda | sed -e 's/.*-standalone-//' -e 's/.jar//')
      export c_name=$config
    #
      echo ";;; -- java -jar $elda -Delda.spec=$PWD/$config_file -----------------------------"
      java -jar $elda -Djetty.port=${_PORT} -Delda.spec=$PWD/$config_file &>> servers.log &
      # sleep to allow the server time to initialise
      sleep 10
    #
      for uri in $(cat uris/$config)
      do
        export u_name=$(echo $uri | sed -e 's/[^A-Za-z0-9._\-]\+/_/g')
        export r_name=elda-${e_name}_${c_name}_${u_name}
        export g_name=${c_name}_${u_name}
      #
        echo ";;; result filename: $r_name"
        echo ";;;;; -- wget http://localhost:8080/$uri"
        wget -O results/${r_name} http://localhost:${_PORT}/$uri 
      done
      pkill -f elda.spec
    done
  done
fi

echo
echo ";;; generating results for current elda ----------------------------------------------------"
echo

export e_name=current

for config_file in configs/*.ttl; do
  echo
  echo ";;; configuration" ${config_file} "------------------------------------------------------"
  echo
  export config=$(echo $config_file | sed -e 's:.*/::' -e 's/\.ttl$//')
  export c_name=${config}

  echo ___ java -jar this-elda/* -Djetty.port=${_PORT} -Delda.spec=$PWD/$config_file

  java -jar this-elda/* -Djetty.port=${_PORT} -Delda.spec=$PWD/$config_file &>> servers.log & 
  sleep 10
#
  for uri in $(cat uris/$config); do
    export u_name=$(echo $uri | sed -e 's/[^A-Za-z0-9._\-]\+/_/g')
    export r_name=elda-${e_name}_${config}_${u_name}

    #
    # patching because the standalone jar's context path changed with 1.2.23
    #
    export standalone_uri=$(echo $uri | sed -e s/elda/standalone/)
    echo ___ wget -O results/${r_name} http://localhost:${_PORT}/$standalone_uri 
    wget -O results/${r_name} http://localhost:${_PORT}/$standalone_uri &> clients.log
    sed -i -e 's:standalone:elda/API:g' results/${r_name}

    for elda in eldas/*.jar; do
      export o_name=$(echo $elda | sed -e 's/.*-standalone-//' -e 's/.jar//')
      export d_file=${e_name}_vs_${o_name}_${c_name}_${u_name}
      echo CONSIDERING ${d_file}

      if multi_diff results/${r_name} results/elda-${o_name}_${c_name}_${u_name} > differences/${d_file}; then
        echo OK -- no differences
      else
        if [ -e gold_differences/${d_file} ]; then
          if multi_diff differences/${d_file} gold_differences/${d_file} > meta_differences/${d_file} ; then
            echo OK -- the same differences as last time
          else
            echo "PROBLEM -- different differences, there's been a change. Oops."
          fi
        else
          echo "OK (ish) -- no existing differences to compare with for" ${d_file}
          cp differences/${d_file} gold_differences/${d_file}
        fi
      fi
    done
  done
  pkill -f elda.spec
done

